Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break up Effects.js #672

Merged
merged 3 commits into from
Dec 14, 2023
Merged

Break up Effects.js #672

merged 3 commits into from
Dec 14, 2023

Conversation

bencodeorg
Copy link
Contributor

@bencodeorg bencodeorg commented Dec 1, 2023

A first step in refactoring our quite large Effects.js file (ie, the code that renders the details of each foreground/background). I put each effect into its own file, and organize into background and foreground effects folders.

I tried to make the changes in this PR as minimal as possible (ie, only moving code between files and including function arguments where needed), but a couple next steps I'm thinking about:

Testing story

I manually tested each background and foreground effect to see that it rendered as expected. We also have some unit testing on effects to make sure that a single frame looks as expected, which all passed.

@bencodeorg bencodeorg requested a review from a team December 1, 2023 17:44
Copy link
Contributor

@sanchitmalhotra126 sanchitmalhotra126 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it! Thanks for diving into this! I know the goal of this PR was to minimize code changes so I'm good with this as is, but I also had a few other follow-ups in mind:

  • Since we're creating new files, can we go ahead and use ES6 import/exports? e.g. export default function(p5...) instead of module.exports = and import instead of require?
  • Thoughts on creating classes for each effect? It would be a little more verbose, but could make parsing the code a little easier? e.g.
class BloomingPetals {
  constructor(p5, getCurrentPalette, colorFromPalette) {
    ...
  }

  init() { ... }
}

then in Effects.js, this.blooming_petals = new BloomingPetals(p5, getCurrentPalette, colorFromPalette)

@fisher-alice
Copy link
Contributor

  • Thoughts on creating classes for each effect? It would be a little more verbose, but could make parsing the code a little easier? e.g.
class BloomingPetals {
  constructor(p5, getCurrentPalette, colorFromPalette) {
    ...
  }

  init() { ... }
}

then in Effects.js, this.blooming_petals = new BloomingPetals(p5, getCurrentPalette, colorFromPalette)

@sanchitmalhotra126 , could you explain why this would be easier to parse?

@sanchitmalhotra126
Copy link
Contributor

sanchitmalhotra126 commented Dec 1, 2023

@sanchitmalhotra126 , could you explain why this would be easier to parse?

It's just more modern syntax - the current format seems like it's the older pre-ES6 version of classes where you'd write it as a function that returns an object, but still use the this keyword to refer to properties and functions declared on the object. I personally find the class syntax a little clearer in displaying what the object properties and functions are, as well as what the this keyword refers to.

Also, thinking ahead to if we were to eventually start using TypeScript in this package, we could declare an interface/abstract class that these classes implement/inherit from. These could define the signatures for some common functions like init and draw for example.

@fisher-alice
Copy link
Contributor

@sanchitmalhotra126 , could you explain why this would be easier to parse?

It's just more modern syntax - the current format seems like it's the older pre-ES6 version of classes where you'd write it as a function that returns an object, but still use the this keyword to refer to properties and functions declared on the object. I personally find the class syntax a little clearer in displaying what the object properties and functions are, as well as what the this keyword refers to.

Gotcha - so it would be easier for humans to read.

Also, thinking ahead to if we were to eventually start using TypeScript in this package, we could declare an interface/abstract class that these classes implement/inherit from. These could define the signatures for some common functions like init and draw for example.

Cool - thanks for the explanation!

Copy link
Contributor

@fisher-alice fisher-alice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love how much easier it is to 'parse' through the Effects.js file.
Looking forward to seeing follow-up steps as well (including @sanchitmalhotra126 's suggestions). Thanks for getting the much-needed refactoring effort started in this repo!

@bencodeorg
Copy link
Contributor Author

@sanchitmalhotra126 agree re: classes, was just easier to copy/paste if I stuck to the existing older syntax.

Re: ES6 import/export, I played with using import / export default and see these errors, is there a babel plugin we're missing or something?

image

Both of these do feel like work items that could be picked up as standalone follow-ups.

@bencodeorg
Copy link
Contributor Author

Oh this is interesting re: Babel config when using dance-party consumed by code-dot-org (this is from code-dot-org config):

// Using babel.config.json instead of .babelrc makes this configuration apply
// even when we are transpiling within node_modules.

https://github.com/code-dot-org/code-dot-org/blob/staging-next/apps/babel.config.json#L1-L2

@fisher-alice
Copy link
Contributor

fisher-alice commented Dec 8, 2023

Oh this is interesting re: Babel config when using dance-party consumed by code-dot-org (this is from code-dot-org config):

// Using babel.config.json instead of .babelrc makes this configuration apply
// even when we are transpiling within node_modules.

https://github.com/code-dot-org/code-dot-org/blob/staging-next/apps/babel.config.json#L1-L2

Just a note that our johnny-five fork also uses module.exports and require. Just curious if you learned anything more about this @bencodeorg

@bencodeorg bencodeorg merged commit 4c7f4e9 into main Dec 14, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants